from numpy import *

def regression(xs, ys):
    '''计算线性回归的系数

    Paramters
    ---------
    xs: numpy.mat
        每一行为一组采样值，不包含常量1
    ys: numpy.array or numpy.mat
        与xs行对应的结果值

    Returns
    -------
    ws: 回归系数
    '''

    return linalg.solve(xs.T*xs, xs.T*ys)

def ridgeRegression(xs, ys, lam=0.2):
    '''计算岭回归的系数
    
    Paramters
    ---------
    xs: numpy.mat
        每一行为一组采样值，不包含常量1
    ys: numpy.array or numpy.mat
        与xs行对应的结果值

    lam: float
        岭回归的lambda系数

    Returns
    -------
    ws: 回归系数
    '''

    return linalg.solve(xs.T*xs + eye(shape(xs)[1])*lam, xs.T*ys)


def localWeightedLinearRegression(x, xs, ys, k):
    from scipy.linalg.misc import norm
    m = shape(xs)[0];
    w = mat(eye(m))

    for i in range(m):
        w[i,i] = exp(norm(x-xs[i,:])/(-2.0*k**2))

    ws = linalg.solve(xs.T*w*xs, xs.T*w*ys)
    return x*ws

def regularize(xMat):
    inMat = xMat.copy()
    inMeans = mean(inMat,0)   #calc mean then subtract it off
    print(inMeans)
    inVar = var(inMat,0)      #calc variance of Xi then divide by it
    print(inMat - inMeans)
    inMat = (inMat - inMeans)/inVar
    return inMat
    
